class UserController < ApplicationController

  #This view is not needed any more.
  def index
    if !logged_in?
      redirect_to :action => "login"
    end 
    @users = User.find(:all)
  end

  #this is kind of like the home page for a user.  It has their account info and the menu.
  def show
    @user = User.find(session[:user])
    #@books = Book.find(:all, :conditions => {:user_id => params[:id]})
    #@wantedBooks = Book.getMyWantedBooks(params[:id])
    #@ownedBooks = Book.getMyBooks(params[:id])
  end
  
  #this is where a user can see the books that belong to him or her.
  def mybooks
    @user = User.find(session[:user])
    #@books = Book.find(:all, :conditions => {:user_id => params[:id]})
    @wantedBooks = Book.getMyWantedBooks(session[:user])
    @ownedBooks = Book.getMyBooks(session[:user])
  end
  
  #this still needs to be done,  This is where good matches from the DB will help users find the books they need.
  def mymatch
    #Match will be the method that searches for books that are a good match for students.
    @matches = Book.find_matched_with_my_owned(session[:user])
  end
  
  #this is the self registratino form view.
  def new
    if logged_in?
      redirect_to :action => "show"
    end 
    @user = User.new
  end
  
  #this is what allows a user to edit them selves.
  def edit
    if logged_in?
      @user = User.find(session[:user])
    else
      redirect_to :action => "show"
    end
  end
  
  #this is the action part of the add view.
  def create

    
    
    @user = User.new(params[:user])
   
    if @user.save #== @user
      @current_user = @user
      session[:user] = @user.id
      
      flash[:notice] = 'You are ready to start trading books'
      redirect_to  :action => "show", :id => @user.id
    else
      flash[:notice] = 'Problem detected'
      render :action => "new"
    end
  end
  
  #this is the action that works with edit.  This is the update function.
  def update
    if logged_in?
      @user = User.find(session[:user])
    else
      redirect_to :controller => "book", :action => "index"
    end
    if @user.update_attributes(params[:user])
      flash[:notice] = 'User successfully updated'
      redirect_to :action => "show", :id =>@user.id
    else
      render :action => "edit"
    end
  end
  
  #this is where we can destroy a user.  Probably better make this an admin function only.
  def destroy
    @user.destroy
    flash[:notice] = 'User account removed'
    redirect_to :action => "index"
  end
  
  #this belongs on the model, not the controller.
  #def find_user
   # @user = User.find(params[:id])
 # end
  
  #form view
  def login
    if logged_in?
      redirect_to :action => "show"
    end 
  end
  
  #this is where a session is created if a user passes authentication.
  def authenticate
    
    @current_user =  User.authenticate(params[:user][:user_name], params[:user][:password]) 
    
    if @current_user
      session[:user] = @current_user.id
      redirect_to  :action  => "show", :id => @current_user.id
      
    else
      flash[:notice] = "No user was found with this email/password "
      render :action => "login"
      
    end
  end
  
  def offers
    @offers_to_me = Offer.get_offers_to_me(session[:user])
    @offers_from_me = Offer.get_offers_from_me(session[:user])
  end
end
